﻿/***********************************************************************
 * <copyright file="FrmXtraBaseTreeDetail.cs" company="BUCA JSC">
 * -->    Copyright (C) statement. All right reserved
 * </copyright>
 * 
 * Created:   LinhMC
 * Email:    linhmc@buca.vn
 * Website:
 * Create Date: Tuesday, February 11, 2014
 * Usage: 
 * 
 * RevisionHistory: 
 * Date         Author               Description 
 * 
 * ************************************************************************/

using System;
using System.ComponentModel;
using System.Globalization;
using System.Threading;
using System.Windows.Forms;
using WindowsForms.AOnline.WindowsForm.CommonClass;
using WindowsForms.AOnline.WindowsForm.Resources;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Mask;


namespace WindowsForms.AOnline.WindowsForm.FormBase
{
    public partial class FrmXtraBaseTreeDetail : XtraForm
    {
        #region Variables

        /// <summary>
        /// 
        /// </summary>
        public enum ActionModeEnum
        {
            AddNew = 0,
            Edit = 1,
            Delete = 2,
            None = 3
        }
        /// <summary>
        /// Gets or sets the e action mode.
        /// </summary>
        /// <value>
        /// The e action mode.
        /// </value>
        public ActionModeEnum ActionMode;
        /// <summary>
        /// The key for send
        /// </summary>
        private string _keyForSend;
        /// <summary>
        /// The has children
        /// </summary>
        public new bool HasChildren;

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the form caption.
        /// </summary>
        /// <value>
        /// The form caption.
        /// </value>
        [Category("BaseProperty")]
        public string FormCaption { get; set; }
        /// <summary>
        /// Gets or sets the key value.
        /// </summary>
        /// <value>
        /// The key value.
        /// </value>
        [Category("BaseProperty")]
        public string KeyValue { get; set; }

        /// <summary>
        /// Gets or sets the name of the key field.
        /// </summary>
        /// <value>
        /// The name of the key field.
        /// </value>
        [Category("BaseProperty")]
        public string KeyFieldName { get; set; }

        /// <summary>
        /// Gets or sets the name of the parent.
        /// </summary>
        /// <value>
        /// The name of the parent.
        /// </value>
        [Category("BaseProperty")]
        public string ParentName { get; set; }

        /// <summary>
        /// Gets or sets the current row.
        /// </summary>
        /// <value>
        /// The current row.
        /// </value>
        [Category("BaseProperty")]
        public object CurrentNode { get; set; }

        #endregion

        #region Functions

        /// <summary>
        /// Initializes the layout.
        /// </summary>
        private void InitializeLayout()
        {
            Text = ActionMode == ActionModeEnum.AddNew ? string.Format(ResourceHelper.GetResourceValueByName("ResAddNewText"), FormCaption) : string.Format(ResourceHelper.GetResourceValueByName("ResEditText"), FormCaption);
            InitControls();
        }
        /// <summary>
        /// Closes the form.
        /// </summary>
        private void CloseForm()
        {
            //using (new FrmBaseCategoryList())
            //{
                if (PostKeyValue != null) PostKeyValue(this, _keyForSend);
                Close();
            //}
        }

        /// <summary>
        /// Formats the control.
        /// </summary>
        protected void FormatControl(Control oContainer)
        {
            foreach (Control control in oContainer.Controls)
            {
                if (control.GetType() == typeof(DateEdit))
                {
              
                    ((DateEdit)control).Properties.Mask.Culture = Thread.CurrentThread.CurrentCulture;
                    ((DateEdit)control).Properties.Mask.UseMaskAsDisplayFormat = true;
                }
                if (control.GetType() == typeof(SpinEdit))
                {
                    if ((string)control.Tag == ControlValueType.Quantity.GetDescription())
                    {
                        ((SpinEdit)control).Properties.EditMask = "n0";
                    }
                    if ((string)control.Tag == ControlValueType.Year.GetDescription())
                    {
                        ((SpinEdit) control).Properties.Mask.MaskType = MaskType.RegEx;
                        ((SpinEdit)control).Properties.EditMask = @"\d{0,4}";
                    }
                    if ((string)control.Tag == ControlValueType.ExchangeRate.GetDescription())
                    {
                        ((SpinEdit)control).Properties.EditMask = "c4";
                    }
                    if ((string)control.Tag == ControlValueType.Percent.GetDescription())
                    {
                        ((SpinEdit)control).Properties.EditMask = "P2";
                    }
                    if ((string)control.Tag == ControlValueType.Money.GetDescription())
                    {
                        ((SpinEdit)control).Properties.EditMask = "c2";
                    }
                    ((SpinEdit)control).Properties.Mask.Culture = Thread.CurrentThread.CurrentCulture;
                    ((SpinEdit)control).Properties.Mask.UseMaskAsDisplayFormat = true;
                }
                if (control.GetType() == typeof(CalcEdit))
                {
                    if ((string)control.Tag == ControlValueType.Quantity.GetDescription())
                    {
                        ((CalcEdit)control).Properties.EditMask = "n0";
                    }
                    if ((string)control.Tag == ControlValueType.Year.GetDescription())
                    {
                        ((CalcEdit)control).Properties.Mask.MaskType = MaskType.RegEx;
                        ((CalcEdit)control).Properties.EditMask = @"\d{0,4}";
                    }
                    if ((string)control.Tag == ControlValueType.ExchangeRate.GetDescription())
                    {
                        ((CalcEdit)control).Properties.EditMask = "c4";
                    }
                    if ((string)control.Tag == ControlValueType.Percent.GetDescription())
                    {
                        ((CalcEdit)control).Properties.EditMask = "P2";
                    }
                    if ((string)control.Tag == ControlValueType.Money.GetDescription())
                    {
                        ((CalcEdit)control).Properties.EditMask = "c2";
                    }
                    ((CalcEdit)control).Properties.Mask.Culture = Thread.CurrentThread.CurrentCulture;
                    ((CalcEdit)control).Properties.Mask.UseMaskAsDisplayFormat = true;
                }
                if (control.Controls.Count > 0)
                {
                    FormatControl(control);
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="keyValue">The key value.</param>
        public delegate void EventPostKeyHandler(object sender, string keyValue);

        /// <summary>
        /// The key value
        /// </summary>
        public event EventPostKeyHandler PostKeyValue;

        #endregion

        #region Functions overrides

        /// <summary>
        /// Shows the help.
        /// </summary>
        protected virtual void ShowHelp()
        {
        }
        /// <summary>
        /// Valids the data.
        /// </summary>
        /// <returns></returns>
        protected virtual bool ValidData()
        {
            return true;
        }
        /// <summary>
        /// Saves the data.
        /// </summary>
        /// <returns></returns>
        protected virtual string SaveData()
        {
            return "";
        }
        /// <summary>
        /// Initializes the data.
        /// </summary>
        protected virtual void InitData()
        {
        }
        /// <summary>
        /// Focuses the control.
        /// </summary>
        protected virtual void InitControls()
        {
        }
        /// <summary>
        /// Automatics the reference no.
        /// </summary>
        protected virtual void AutoRefNo()
        {
        }

        #endregion

        public FrmXtraBaseTreeDetail()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Handles the Load event of the FrmXtraBaseTreeDetail control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void FrmXtraBaseTreeDetail_Load(object sender, EventArgs e)
        {
            try
            {
                if (DesignMode) return;
                InitializeLayout();
                InitData();
                FormatControl(this);
                if (ActionMode == ActionModeEnum.AddNew) AutoRefNo();
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, ResourceHelper.GetResourceValueByName("ResExceptionCaption"), MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// Handles the Click event of the btnSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (ValidData())
                {
                    var rowAffected = SaveData();
                    //if (rowAffected > 0)
                    //{
                    //    _keyForSend = ActionMode == ActionModeEnum.AddNew
                    //                      ? rowAffected.ToString(CultureInfo.InvariantCulture)
                    //                      : KeyValue;
                    //    CloseForm();
                    //}
                    if (rowAffected == null)
                    {
                        if (ActionMode != ActionModeEnum.AddNew)
                        {
                            _keyForSend = KeyValue;
                        }
                        else
                        {
                            //   _keyForSend = ActionMode == ActionModeEnum.AddNew ? rowAffected.ToString(CultureInfo.InvariantCulture) : KeyValue;
                        }                      
                        CloseForm();
                    }
                    else
                        XtraMessageBox.Show(ResourceHelper.GetResourceValueByName("ResSaveDataError"),
                                            ResourceHelper.GetResourceValueByName("ResDetailContent"),
                                            MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, ResourceHelper.GetResourceValueByName("ResExceptionCaption"), MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// Handles the Click event of the btnExit control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void btnExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnHelp_Click(object sender, EventArgs e)
        {
            ShowHelp();
        }
    }
}